package com.tracy.lifeproject.discovery.goods.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.activity.DescriptionActivity;
import com.tracy.lifeproject.bean.GoodsCategoryBean;
import com.tracy.lifeproject.discovery.goods.adapter.GoodsGridViewAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class GoodsItemActivity extends AppCompatActivity {

    @InjectView(R.id.img_simple_back)
    ImageView imgSimpleBack;
    @InjectView(R.id.btn_simple_back)
    Button btnSimpleBack;
    @InjectView(R.id.text_simple_toolbar)
    TextView textSimpleToolbar;
    @InjectView(R.id.img_toolbar_simple)
    ImageView imgToolbarSimple;
    @InjectView(R.id.toolbar_simple)
    Toolbar toolbarSimple;
    @InjectView(R.id.girdView_simple)
    GridView girdViewSimple;

    private List<GoodsCategoryBean.DataBean.ItemsBean> mDatas;
    private GoodsGridViewAdapter adapter;
    private String url;
    private String itemName;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);//直接使用现有的布局
        ButterKnife.inject(this);

        mDatas = new ArrayList<>();
        mActivity = this;

        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        itemName = intent.getStringExtra("itemName");

        if (itemName != null) {
            Log.i("zjs", itemName);
            textSimpleToolbar.setText(itemName);
        }

        if (url != null) {
            loadDatas(url);
        }
        adapter = new GoodsGridViewAdapter(this, mDatas);
        girdViewSimple.setAdapter(adapter);

        girdViewSimple.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, DescriptionActivity.class);
                intent.putExtra("id", mDatas.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void loadDatas(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(GoodsItemActivity.this, "商品加载失败，请重试", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                GoodsCategoryBean goodsCategoryBean = new Gson().fromJson(result, GoodsCategoryBean.class);
                mDatas.addAll(goodsCategoryBean.getData().getItems());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    @OnClick({R.id.btn_simple_back,R.id.img_simple_back, R.id.img_toolbar_simple})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_simple_back:
                finish();
                break;
            case R.id.img_simple_back:
                finish();
                break;
            case R.id.img_toolbar_simple:
                //菜单栏点击事件
                showPopupMenu(imgToolbarSimple);
                break;
        }
    }
    /**
     * 点击出现菜单
     */
    private void showPopupMenu(ImageView imgToolbarSimple) {
        PopupMenu popupMenu = new PopupMenu(this, imgToolbarSimple);
        // menu布局
        popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
        // menu的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        // PopupMenu关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
                //Toast.makeText(getApplicationContext(), "关闭PopupMenu", Toast.LENGTH_SHORT).show();
            }
        });
        popupMenu.show();
    }
}
